#include"Date.h"

bool Date::operator< (const Date& d) const
{
    if(_year != d._year) return _year < d._year;
    if(_month != d._month) return _month < d._month;
    return _day < d._day;
}

bool Date::operator== (const Date& d) const
{
    return _day == d._day && _month == d._month && _year == d._year;
}

bool Date::operator<= (const Date& d) const
{
    return *this < d || *this == d;
}

bool Date::operator> (const Date& d) const
{
    return !(*this <= d); // 不<=就是大于
}

bool Date::operator>= (const Date& d) const
{
    return !(*this < d);
}

bool Date::operator!= (const Date& d) const
{
    return !(*this == d);
}

Date& Date::operator++ ()
{
    _day++;
    if(_day > get_day_of_month(_year, _month))
    {
        _day = 1;
        _month++;
        if(_month == 13)
        {
            _month = 1;
            _year++;
        }
    }
    return *this;
}

Date& Date::operator-- ()
{
    _day--;
    if(_day == 0)
    {
        _month--;
        if(_month == 0)
        {
            _month = 12;
            _year--;
        }
        _day = get_day_of_month(_year, _month);
    }
    return *this;
}

Date Date::operator++ (int)
{
    Date t = *this;
    _day++;
    if(_day > get_day_of_month(_year, _month))
    {
        _day = 1;
        _month++;
        if(_month == 13)
        {
            _month = 1;
            _year++;
        }
    }
    return t;
}

Date Date::operator-- (int)
{
    Date t = *this;
    _day--;
    if(_day == 0)
    {
        _month--;
        if(_month == 0)
        {
            _month = 12;
            _year--;
        }
        _day = get_day_of_month(_year, _month);
    }
    return t;
}


Date& Date::operator+= (int n)
{
    if(n >= 0)
    {
        while(n--)
            (*this) ++;
    }
    else 
        (*this) -= -n;
    return *this;
}
Date& Date::operator-= (int n)
{
    if(n >= 0)
    {
        while(n--)
            (*this) --;
    }
    else 
        (*this) += -n;
    return *this;
}

Date Date::operator+ (int n) const
{
    Date t = *this;
    t += n;
    return t;
}

Date Date::operator-(int n) const
{
    Date t = *this;
    t -= n;
    return t;
}

int Date::operator- (const Date& d) const
{
    // 大 - 小：天数为正；反之为负
    int flag = 1;
    Date begin = d;
    Date end = *this;
    if(*this < d) 
    {
        flag = -1;
        begin = *this, end = d;
    }
    int cnt = 0;
    while(begin != end)
    {
        cnt++;
        begin++;
    }
    return cnt * flag;
}

ostream& operator<< (ostream& out, Date& d)
{
    out << d._year << '-' << d._month << '-' << d._day;
    return out;
}

istream& operator>> (istream& in, Date& d)
{
    in >> d._year >> d._month >> d._day;

    // 额外检查日期是否合法
    if(d._year <= 0 || d._month <= 0 || d._month > 12 || d._day <= 0 || d._day > d.get_day_of_month(d._year, d._month))
    {
        puts("输入日期非法");
        exit(-1);
    }
    return in;
}
